# 读取文件
$pushlist = Get-Content .\push_list.txt

# 重新以只读挂载分区
& hdc shell mount -o rw,remount /

# 循环处理每一行
foreach ($line in $pushlist) {
    # 解析文件路径
    $localFile,$deviceFile = $line -split ' '

    # 将本地文件路径中的'/'替换成'\'
    $localFile = $localFile -replace '/','\'

    # 将设备文件路径中的'\'替换成'/'
    $deviceFile = $deviceFile -replace '\\','/'

    # 判断本地文件是否存在
    if (Test-Path $localFile) {
        # 判断设备中是否存在文件
        $fileExists = & hdc shell "test -e '$deviceFile' && echo 1 || echo 0"
        if ($fileExists -eq "1") {
            # push file to device 
            Write-Output "$localFile ===> $deviceFile"
            & hdc file send "$localFile" "$deviceFile"

            # 生成本地和远端文件的mD5值
            $localFileMd5 = (Get-FileHash $localFile -Algorithm MD5).Hash.ToLower()
            $deviceFileMd5 = & hdc shell "md5sum $deviceFile" | ForEach-Object{$_.Split(" ")[0]} | ForEach-Object{$_.ToLower()}
            # 比较两个MD5值是否一致
            if ($localFileMd5 -ne $deviceFileMd5) {
                Write-Output "Erroe: File transfer error"
            }
        } else {
            Write-Output "device is not exist $deviceFile"
        }
    } else {
        Write-Output "$localFile is not exist!!!"
    }
    Write-Output ""
}

# 关机
& hdc shell reboot

# 等待开机
& hdc wait-for-device

# 关闭日志流控
& hdc shell hilog -Q pidoff ; & hdc shell hilog -Q domainoff